"""
Case Type   : mot不支持的索引
Case Name   : MOT表不支持在总列宽>256的表上创建索引，数据类型为float4
Create At   : 2020/09/18
Owner       : opentestcase001
Description :
    1.设置enable_incremental_checkpoint参数为off,并重启数据库
    2.MOT表不支持在总列宽>256的表上创建索引，数据类型为float4
      键大小包括以字节为单位的列大小+列附加大小，其是维护索引所需的开销(详见表格不同类型对应的列附加大小)
      如果索引不是唯一的，需要额外的8字节
    3.恢复环境默认配置，修改enable_incremental_checkpoint为on，并重启数据库
Expect      :
    1.设置成功，重启数据库成功
    2.普通索引创建失败，唯一索引创建失败
    3.修改参数成功，重启数据库成功
History     :
   modified by opentestcase010 at 2023/2/2，MOT表支持在总列宽>256的表上创建索引，数据类型为float4
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Mot_datatype_test(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.commonsh = CommonSH
        self.common = Common()
        self.config = 'enable_incremental_checkpoint'
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('-----修改配置，并重启数据库-----')
        show_msg = self.common.show_param(self.config)
        if 'off' not in show_msg:
            msg1 = self.primary_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'{self.config}=off')
            self.logger.info(msg1)
            msg2 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg2)
            msg3 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg3)
            show_msg = self.common.show_param(self.config)
            self.assertTrue('off' in show_msg)

    def test_mot_none_index(self):
        text = '----step:mot支持在总列宽>256的表上创建索引，数据类型为float4;' \
               'expect:成功----'
        self.logger.info(text)
        self.schema = 'schema_mot_0084'
        self.t_name = 't_mot_0084'
        self.t_name_1 = 't_mot_0084_1'
        self.sql_cmd1 = f'''
            drop schema if exists {self.schema} cascade;
            create schema {self.schema};
            create foreign table {self.schema}.{self.t_name}
            (t1 float4 not null,t2 float4 not null,
            t3 float4 not null,t4 varchar(233) not null);
            create index index1 on {self.schema}.{self.t_name}(t1,t2,t4);
                      '''
        msg4 = self.primary_sh.execut_db_sql(self.sql_cmd1)
        self.logger.info(msg4)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg4)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, msg4)
        self.sql_cmd2 = f'''
        create foreign table {self.schema}.{self.t_name_1}
        (t1 float4 not null,t2 float4 not null,
        t3 float4 not null,t4 varchar(241) not null);
        create unique index index4 on {self.schema}.{self.t_name_1}
        (t1,t3,t4);
        drop schema {self.schema} cascade;
                      '''
        msg5 = self.primary_sh.execut_db_sql(self.sql_cmd2)
        self.logger.info(msg5)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg5)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, msg5)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG, msg5)

    def tearDown(self):
        self.logger.info('----this is tearDown----')
        self.logger.info('----恢复配置，enable_incremental_checkpoint=on----')
        show_msg = self.common.show_param(self.config)
        if 'on' not in show_msg:
            msg6 = self.primary_sh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f'{self.config}=on')
            self.logger.info(msg6)
            msg7 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg7)
            msg8 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg8)
        show_msg = self.common.show_param(self.config)
        self.assertTrue('on' in show_msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
